package com.bruce.principle.openclose;

import com.bruce.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

/**
 * Created by bruce on 2018/10/21 23:00
 */
@Slf4j
public class MainTest {

    @Test
    public void test1() {
        //原业务逻辑
        ICourse javaCourse = new JavaCourse(96, "java高并发实战", 38.8);
        log.info(JsonUtils.toJson(javaCourse));

        //打折业务,使用继承并覆盖价格方法计算折后价,而不是修改原方法
        //如果要获原价格,可以在子类JavaDiscountCourse中添加新方法返回原价格
        ICourse javaDiscountCourse = new JavaDiscountCourse(96, "java高并发实战", 38.8);
        log.info(JsonUtils.toJson(javaDiscountCourse));

        JavaDiscountCourse course = (JavaDiscountCourse) javaDiscountCourse;
        log.info("课程原价:" + course.getOriginPrice());

    }

    //满足里氏替换原则
    @Test
    public void test2() {
        //原业务逻辑
        ICourse javaCourse = new JavaCourse(96, "java高并发实战", 38.8);
        log.info(JsonUtils.toJson(javaCourse));

        //打折业务,使用继承并覆盖价格方法计算折后价,而不是修改原方法
        //如果要获原价格,可以在子类JavaDiscountCourse中添加新方法返回原价格
        ICourse javaDiscountCourse = new JavaDiscountCourse2(96, "java高并发实战", 38.8);
        log.info(JsonUtils.toJson(javaDiscountCourse));

        JavaDiscountCourse2 course = (JavaDiscountCourse2) javaDiscountCourse;
        log.info("课程原价:" + course.getDiscountPrice());

    }


}
